<template>
	<base-right-dialog ref="baseRightDialog" :footerShow="true" :dialogVisible.sync="dialogVisible" :appendBody="true"
		:title="dialogTitle + ' 角色'" @handleClose="handleDialogClose" @handleConfirmClick="handleConfirmClick">
		<base-table ref="customtable" :showSelect="true" @onSelectionChange="onSelectionChange"
			:tabLoading.sync="tabLoading" :tableHeight="tableHeight" :tableData="tableData" :tableColumn="tableColumn">
			<!-- 所属分类 -->
			<template v-slot:category="{row}">
				<p>{{row.category == '1'?'超级管理员':row.category == '2'?'管理员':'业务员'}}</p>
			</template>
			<!-- 角色分类 -->
			<template v-slot:roleType="{row}">
				<p>{{row.roleType | roleType}}</p>
			</template>
			<!-- 所属分类 -->
			<template v-slot:roleBond="{row}">
				<p>{{row.roleBond | roleBond}}</p>
			</template>
			<template v-slot:isBenchmark="{row}">
				<p>{{row.isBenchmark =='1'?'是':'否'}}</p>
			</template>
			<template v-slot:externalType="{row}">
				<p>{{row.externalType =='1'?'外部':'内部'}}</p>
			</template>
			<template v-slot:isEnable="{row}">
				<p>{{row.isEnable =='1'?'显示':'隐藏'}}</p>
			</template>
		</base-table>
	</base-right-dialog>
</template>

<script>
	import baseRightDialog from "@/components/base/baseRightDialog";
	import baseTable from "@/components/base/baseTable";
	import roleConfigData from "@/views/systemSettings/userSettings/roleSettings/configData";

	import {
		authApi
	} from '@/api/apis/auth'
	export default {
		components: {
			baseRightDialog,
			baseTable
		},
		props: {
			orgData: {
				type: Array,
				default: []
			}
		},
		data() {
			return {
				dialogTitle: '新增',
				dialogVisible: false,
				tableColumn: roleConfigData.tableColumn,
				tableHeight: '500px',
				tableData: [],
				tabLoading: false,
				selectTable: []
			}
		},
		filters: {
			roleType(val) {
				let roleTypeName = ''
				switch (val) {
					case '1':
						roleTypeName = '固定角色'
						break;
					case '2':
						roleTypeName = '相对角色'
						break;
					case '3':
						roleTypeName = '自建角色'
						break;
				}
				return roleTypeName
			},
			roleBond(val) {
				let roleBondName = ''
				switch (val) {
					case '0':
						roleBondName = '集团角色'
						break;
					case '1':
						roleBondName = '单位角色'
						break;
					case '2':
						roleBondName = '部门角色'
						break;
					case '3':
						roleBondName = '不使用'
						break;
					case '4':
						roleBondName = '不使用'
						break;
					case '5':
						roleBondName = '业务生成器'
						break;
					case '6':
						roleBondName = 'sso'
						break;
				}
				return roleBondName
			}
		},
		methods: {
			openDialog( userId) {
				this.dialogVisible = true
				this.userId = userId
				this.getTableList()
				this.getRole(userId)
			},
			async getRole(userId){
				let res = await authApi('userService', 'user', 'queryEntityRole', '', {id:userId})
			},
			async getDetail(id) {
				this.openLoading('detail')
				let res = await authApi('userService', 'user', 'queryEntity', '', {
					id: id
				})
				if (res.status == '200') {
					this.$refs.basicsForm.choiceAssignment(res.attribute)
				}
			},
			// 获取表格内容
			async getTableList() {
				this.radioIndex = false
				this.tabLoading = true
				setTimeout(() => {
					this.tabLoading = false
				}, 100000)
				let params = {
					"pageNum": 1,
					"pageSize": 999999
				}
				let res = await authApi('roleService', 'role', 'queryList', '', params)
				if (res.status == '200') {
					this.$refs.customtable.clearRadioIndex()
					this.tableData = res.attribute.list
				}
				this.tabLoading = false
			},
			onSelectionChange(selectTable) {
				this.selectTable = selectTable
			},
			async handleConfirmClick() {
				let userRoles = []
				this.selectTable.forEach(el => {
					let arr = {
						userId: this.userId,
						roleId: el.id
					}
					userRoles.push(arr)
				})
				let params = {
					id: this.userId,
					userRoles: userRoles
				}
				let res = await authApi('userService', 'user', 'addOrUpdateUserRole', '', params)
				if (res.status == '200') {
					this.$vmNews('保存成功', 'success')
					this.handleDialogClose()
				}

			},
			handleDialogClose() {
				this.dialogVisible = false
				this.$emit('handleDialogClose')
			},

		}
	}
</script>

<style>
</style>
